<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="naogen" />
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作
                    <i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 使用el-tree放主体的内容 -->
        <el-tree :data="list">
          <template #default="{data}">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px;width:100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="naogen" />
                <span>{{data.name}}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{data.manager}}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作
                        <i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <!-- <el-dropdown-item @click.native="hAdd('')">添加部门</el-dropdown-item> -->
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 实现新增子部门或者编辑部门的对话框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="resetForm"
      >
        <!-- 在弹层中放入子组件 -->
        <addOrEdit 
        :id="curId" 
        ref="addOrEditRef" 
        :is-edit="isEdit" 
        :origin-list="originList"
        @success="hSuccess" 
        @close="showDialog=false"/>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import addOrEdit from "./deptDialog.vue";
import { getDepartments, delDepartment } from "@/api/departments";
import { tranDataToTree } from "@/utils";
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      showDialog: false,
      list: [],
      curId: "",
      isEdit: false,//false 新增子部门   true 编辑部门
      originList:[]//保存所有部门列表的部分(id,pid,name,code)数据
    };
  },
  created() {
    this.loadDepartments();
  },
  methods: {
    //充值子组件中的表单
    resetForm(){
      this.$refs.addOrEditRef.resetForm()
    },
    //删除部门
    hDel(id) {
      this.$confirm("此操作将永久删除部门, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then( async () => {
          // 确定
          await delDepartment(id)
          // 重新渲染
          this.loadDepartments()
        })
        .catch(() => {
          this.$message({
            type:'info',
            message:'已取消删除'
          })
        });
    },
    hSuccess() {
      this.loadDepartments();
      this.showDialog = false;
    },
    //编辑部门
    hEdit(id) {
      this.curId = id;
      this.isEdit = true;
      this.showDialog = true;
      //dom更新是异步
      this.$nextTick(() => {
        //解决数据不更新的问题：方法二： this.$refs.addOrEdiRef 可以获取子组件，可以直接操作子组件里面的一切
        console.log(this.$refs.addOrEditRef, "addOrEditRef");
        this.$refs.addOrEditRef.loadDepartmentDetail();
      });
    },
    hAdd(id) {
      this.curId = id;
      this.isEdit = false;
      this.showDialog = true;
    },
    //加载部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments();
        console.log("getDepartments", res);
        
        this.list = tranDataToTree(res.data.depts);
        //获取部门数据并取出 数组中每个对象的部门数据(id,pid,name,code)
        this.originList= res.data.depts.map(item=>{
          return {id:item.id,pid:item.pid,name:item.name,code:item.code}
          
        })
        //删除数组中第一条数据，取出公司的名称
        res.data.depts.shift();
      } catch (error) {
        console.log(error);
      }
    },
    handleNodeClick(data) {
      console.log(data);
    }
  }
};
</script>